<?php
session_start();

require_once '../include/config.lib.php';
require_once '../include/database.lib.php';
require_once '../include/friendAPI.php';

if($_SESSION["personid"] == null)
{
	$_SESSION["errmsg"] = "Please log in first.";
	echo "<HTML><META http-equiv=\"refresh\" content=\"0; url=../login.php\"></HTML>";//redirect to the login page
}

else 
{
	// Connect to the DB.
	ConnectToDB();
	//select the person's password from database
	//$passwordReq = "SELECT password FROM PERSON WHERE USERNAME = '".$_SESSION["accountNo"]."'";
	$passwordResult = DBExecute("SELECT password FROM PERSON WHERE USERNAME = ?", $_SESSION["accountNo"]);
	$password = $passwordResult->fetchAssocRow();

	$data = array("jaxbBean"=>array(array("key"=>"requester", "val"=>$_SESSION["accountNo"]), array("key"=>"requested", "val"=>$_SESSION["viewperson"]), array("key"=>"password", "val"=>$password["PASSWORD"])));
	$post_str = json_encode($data);
	$url = $APIhost.'publishService/friends/follow';
	$result = sendToHost($url, "POST", $post_str, 'application/json', 'text/plain');

	if($result == 1)
	{
		echo "<HTML><META http-equiv=\"refresh\" content=\"0; url='".$_SESSION["previouspage"]."'\"></HTML>";//redirect to the previous page
	}
	elseif($result == 2)
	{
		?>
		<script>alert("This person doesn't exit!");</script>
		<?php
		echo "<HTML><META http-equiv=\"refresh\" content=\"0; url='".$_SESSION["previouspage"]."'\"></HTML>";//redirect to the previous page
	}
	elseif($result == 3)
	{
		?>
		<script>alert("Authentication Failed! Please try again.");</script>
		<?php
		echo "<HTML><META http-equiv=\"refresh\" content=\"0; url='".$_SESSION["previouspage"]."'\"></HTML>";//redirect to the previous page
	}
	elseif($result == 4)
	{
		echo "<HTML><META http-equiv=\"refresh\" content=\"0; url='".$_SESSION["previouspage"]."'\"></HTML>";//redirect to the previous page
	}
	elseif($result == 5)
	{
		?>
		<script>alert("Sorry, Internal Error. Please contact our Administrator.");</script>
		<?php
		echo "<HTML><META http-equiv=\"refresh\" content=\"0; url='".$_SESSION["previouspage"]."'\"></HTML>";//redirect to the previous page
	}
}

?>